<template>
    <el-dialog :model-value="dialogVisible" :close-on-click-modal="true" :before-close="handleClose" :title="isAdd === 'add' ? '新增字段值' : '修改字段值'" append-to-body width="520px">
        <el-form ref="formRef" :model="form" label-width="120px">
            <el-form-item v-for="(item,index) in dataValueList" :label="item.fieldName" :prop="item.fieldCode" :rules="rulesVild(item)" :key="index">
                <el-date-picker
                    v-if="item.fieldType === 'Date'"
                    v-model="form[item.fieldCode]"
                    type="date"
                    value-format="YYYY-MM-DD"
                    :placeholder="'请选择'+ item.fieldName"
                    class="w100p"
                >
                </el-date-picker>
                <el-select v-else-if="item.fieldType === 'Boolean'" v-model="form[item.fieldCode]" filterable :placeholder="'请选择' + item.fieldName" class="w100p">
                    <el-option label="true" value="true"/>
                    <el-option label="false" value="false"/>
                </el-select>
                <el-input v-else v-model="form[item.fieldCode]" :placeholder="'请输入' + item.fieldName" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button :loading="loading" type="primary" @click="submit()">确定</el-button>
                 <el-button @click="handleClose">取消</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script  setup>
import { fieldTypeOption, onlyOption } from './config'
import { ElMessage } from 'element-plus'
// eslint-disable-next-line no-unused-vars
const props = defineProps({
    dialogVisible: {
        type: Boolean,
        default: false
    },
    isAdd: {
        type: String,
        default: 'add'
    },
    formData: {
        type: Object,
        default: {}
    },
    columns: {
        type: Array,
        default: []
    }
})

watch(() => props.dialogVisible,(newVal) => {
    if(newVal === true && props.isAdd === 'add'){
        dataValueList.value = [...props.columns]
        const newObj = {}
        dataValueList.value.forEach((item)=>{
            newObj[item.fieldCode] = ''
        })
        form.value = newObj
    }
    if(newVal === true && props.isAdd === 'edit'){
        dataValueList.value = [...props.columns]
        const newObj = {}
        dataValueList.value.forEach((item)=>{
            newObj[item.fieldCode] = ''
        })
        form.value = {...props.formData}
    }
})

const emit = defineEmits(['on-submit', 'handleClose'])
const formRef = ref(null)
const dataValueList = ref([])
const form = ref({})
const options = ref([])

const loading = ref(false)
const handleClose = () => {
    formRef.value.resetFields()
    emit('handleClose',false)
}
const submit = () => {
    if (!formRef) return
    formRef.value.validate(valid => {
        if (valid) {
            emit('on-submit', {...form.value} , props.isAdd)
            handleClose()
        } else {
            return false
        }
    })

}

const validatedecimal = (rule, value, callback) => {
    console.log(rule)
    console.log(value)
    callback(new Error('26个英文字母和0-9的自然数+下划线组成，首字符为字母'))
    //callback()
}

const rulesVild = (item)=>{
    if(item.fieldType === 'String'){
        return [
            { max: Number(item.fieldLength), message: item.fieldName + '最长为' + item.fieldLength + '个字符', trigger: 'blur' }
        ]
    }
    if(item.fieldType === 'Integer'){
        return [
            { max: Number(item.fieldLength), message: item.fieldName + '最长为' + item.fieldLength + '个字符', trigger: 'blur' },
            {
                pattern: /^[+]{0,1}(\d+)$/,
                message: item.fieldName + '只能为正整数',
                trigger: 'blur'
            }
        ]
    }
    if(item.fieldType === 'decimal'){
        const zlength = item.fieldLength.split(',')[0]
        const xlength = item.fieldLength.split(',')[1]
        const ruleString = new RegExp("^\\d+(\\.\\d{1," + xlength + "})?$");
        
        console.log(ruleString)
        return [
            {
                pattern: ruleString,
                message: '请输入正确格式,最多' + zlength + '位,小数最多' + xlength +'位',
                trigger: 'blur'
            },
            { max: Number(zlength), message: item.fieldName + '最长为' + zlength + '个字符', trigger: 'blur' }
        ]
    }
}

</script>

<style lang="scss" scoped>
.dialog-footer{
    text-align: right;
}
</style>
